#!/usr/bin/with-contenv bashio

# 工具函数文件

# 验证 Gitee URL
validate_gitee_url() {
    local url=$1
    if [[ ! $url =~ ^https://gitee\.com/.+/.+ ]]; then
        bashio::log.error "无效的 Gitee 仓库地址"
        return 1
    fi
    return 0
}

# 转换为原始内容 URL
get_raw_url() {
    local repo_url=$1
    # 移除末尾的斜杠（如果有）
    repo_url=${repo_url%/}
    echo "${repo_url}/raw"
}

# 确保目录存在
ensure_directories() {
    local www_dir=$1
    local community_dir=$2
    local storage_dir=$3
    
    mkdir -p "${www_dir}" "${community_dir}" "${storage_dir}"
    if [ $? -ne 0 ]; then
        bashio::log.error "创建目录失败"
        return 1
    fi
    return 0
}

# 初始化资源文件
init_resources_file() {
    local resources_file=$1
    if [ ! -f "${resources_file}" ]; then
        echo '{"version":1,"minor_version":1,"key":"lovelace_resources","data":{"items":[]}}' > "${resources_file}"
        if [ $? -ne 0 ]; then
            bashio::log.error "初始化资源文件失败"
            return 1
        fi
    fi
    return 0
}